
    /**  
    * @Title: InitHttpServiceConfig.java
    * @Package com.efast.cafe.commponent.es.http.configuration
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2018年10月19日
    * @version V1.0  
    */
    
package com.efast.cafe.commponent.es.http.configuration;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ScannedGenericBeanDefinition;
import org.springframework.context.event.ContextRefreshedEvent;

import com.efast.cafe.commponent.es.http.annotation.CafeHTTPService;
import com.efast.cafe.commponent.es.http.constant.HttpConstant;
import com.efast.cafe.commponent.es.webservice.annotation.CafeWSService;
import com.efast.cafe.commponent.es.webservice.constant.WSConstant;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.util.BeanDefinitionFilter;
import com.efast.cafe.util.ConfigUtil;
import com.efast.cafe.util.SpringUtils;
import com.efast.cafe.util.StringUtil;

/**
    * @ClassName: InitHttpServiceConfig
    * @Description: 初始化对外http接口适配器
    * @author Administrator
    * @date 2018年10月19日
    *
    */
@ConditionalOnProperty(
        name="http.enable",
        havingValue="true"
)
@Configuration
public class InitHttpServiceConfig implements ApplicationListener<ContextRefreshedEvent>{

	public  Logger logger = LoggerFactory.getLogger(getClass());
	private boolean isReady = false;
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		try{
			ApplicationContext ctx = event.getApplicationContext();
			if(!isReady){
				scanHTTPServiceConfig(ctx);
				isReady = true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private void scanHTTPServiceConfig(ApplicationContext ctx)throws Exception{
		String basePackage = ConfigUtil.getValue(HttpConstant.HTTP_BASEBACKAGE);
		if(StringUtil.isNotBlank(basePackage)){
			String[] basePackages = basePackage.split(",");
			SpringUtils springUtils = SpringUtils.getInstance(ctx);
			for(String packageStr : basePackages){
				
				Set<BeanDefinition> candidateComponents = springUtils.findCandidateComponents(packageStr,
						new BeanDefinitionFilter() {
							public boolean filter(ScannedGenericBeanDefinition sbd) {
								try {
									GenericBeanDefinition definition = sbd;
									Class<?> entityClazz = Class.forName(definition.getBeanClassName());
									
									CafeHTTPService cafeHttpServicee = entityClazz.getAnnotation(CafeHTTPService.class);
									if(cafeHttpServicee==null){
										return false;
									}
									return true;
								} catch (ClassNotFoundException e) {
									e.printStackTrace();
								}
								return false;
							}
						});
				
				for (BeanDefinition beanDefinition : candidateComponents) {
					GenericBeanDefinition definition = (GenericBeanDefinition) beanDefinition;
					Class<?> interfaceClazz = Class.forName(definition.getBeanClassName());
					CafeHTTPService cafeHTTPService=interfaceClazz.getAnnotation(CafeHTTPService.class);
					String key = cafeHTTPService.bizCode()+cafeHTTPService.version();
					String v = HttpConstant.getHttpServiceClass(key);
					if(StringUtil.isNotBlank(v)){
						throw new ServiceException(v+"存在相同的bizCode");
					}
					String className = interfaceClazz.getName();
					HttpConstant.setHttpServiceClass(key, className);
					logger.info("Load HttpService Class :"+className);
				}
				
			}
		}
		
	}
}
